﻿namespace DistrRTgen
{
    using System;
    using System.IO;
    using System.Xml;

    public static class Settings
    {
        private static SerializableDictionary<string, string> _Settings;

        public static void Load()
        {
            _Settings = new SerializableDictionary<string, string>();
            if (File.Exists("app.config"))
            {
                XmlReaderSettings xrs = new XmlReaderSettings();
                xrs.ConformanceLevel = ConformanceLevel.Fragment;
                XmlReader xr = XmlReader.Create("app.config", xrs);
                _Settings.ReadXml(xr);
                xr.Close();
            }
            if (!_Settings.ContainsKey("BaseURL"))
            {
                _Settings.Add("BaseURL", "http://distributed.freerainbowtables.com/");
            }
            if (!_Settings.ContainsKey("cmdArchitecture"))
            {
                _Settings.Add("cmdArchitecture", "");
            }
            if (!_Settings.ContainsKey("cmdVersion"))
            {
                _Settings.Add("cmdVersion", "");
            }
            if (!_Settings.ContainsKey("Username"))
            {
                _Settings.Add("Username", "");
            }
            if (!_Settings.ContainsKey("Password"))
            {
                _Settings.Add("Password", "");
            }
            if (!_Settings.ContainsKey("ClientID"))
            {
                _Settings.Add("ClientID", "0");
            }
            if (!_Settings.ContainsKey("AcceptNewParts"))
            {
                _Settings.Add("AcceptNewParts", Convert.ToString(true));
            }
            if (!_Settings.ContainsKey("UploadParts"))
            {
                _Settings.Add("UploadParts", Convert.ToString(true));
            }
            if (!_Settings.ContainsKey("UploadStartTime"))
            {
                _Settings.Add("UploadStartTime", Convert.ToString(Convert.ToDateTime("00:00")));
            }
            if (!_Settings.ContainsKey("UploadEndTime"))
            {
                _Settings.Add("UploadEndTime", Convert.ToString(Convert.ToDateTime("23:59")));
            }
            if (!_Settings.ContainsKey("EventLogThreshold"))
            {
                _Settings.Add("EventLogThreshold", LoggerLevel.LOG_ALERT.ToString());
            }
            if (!_Settings.ContainsKey("EnablePublicHashCracking"))
            {
                _Settings.Add("EnablePublicHashCracking", Convert.ToString(true));
            }
            if (!_Settings.ContainsKey("WorkStartTime"))
            {
                _Settings.Add("WorkStartTime", Convert.ToString(Convert.ToDateTime("00:00")));
            }
            if (!_Settings.ContainsKey("WorkEndTime"))
            {
                _Settings.Add("WorkEndTime", Convert.ToString(Convert.ToDateTime("23:59")));
            }
            if (!_Settings.ContainsKey("NumCPUToUse"))
            {
                _Settings.Add("NumCPUToUse", Convert.ToString(Environment.ProcessorCount));
            }
        }

        public static void Save()
        {
            XmlWriterSettings xws = new XmlWriterSettings();
            xws.ConformanceLevel = ConformanceLevel.Fragment;
            XmlWriter xw = XmlWriter.Create("app.config", xws);
            _Settings.WriteXml(xw);
            xw.Close();
        }

        public static bool AcceptNewParts
        {
            get
            {
                return Convert.ToBoolean(_Settings["AcceptNewParts"]);
            }
            set
            {
                _Settings["AcceptNewParts"] = Convert.ToString(value);
                Save();
            }
        }

        public static string BaseURL
        {
            get
            {
                return _Settings["BaseURL"];
            }
            set
            {
                _Settings["BaseURL"] = value;
                Save();
            }
        }

        public static int ClientID
        {
            get
            {
                return Convert.ToInt32(_Settings["ClientID"]);
            }
            set
            {
                _Settings["ClientID"] = Convert.ToString(value);
                Save();
            }
        }

        public static string cmdArchitecture
        {
            get
            {
                return _Settings["cmdArchitecture"];
            }
            set
            {
                _Settings["cmdArchitecture"] = value;
                Save();
            }
        }

        public static string cmdVersion
        {
            get
            {
                return _Settings["cmdVersion"];
            }
            set
            {
                _Settings["cmdVersion"] = value;
                Save();
            }
        }

        public static bool EnablePublicHashCracking
        {
            get
            {
                return Convert.ToBoolean(_Settings["EnablePublicHashCracking"]);
            }
            set
            {
                _Settings["EnablePublicHashCracking"] = Convert.ToString(value);
                Save();
            }
        }

        public static LoggerLevel EventLogThreshold
        {
            get
            {
                if ((_Settings != null) && _Settings.ContainsKey("EventLogThreshold"))
                {
                    return (LoggerLevel) Enum.Parse(typeof(LoggerLevel), _Settings["EventLogThreshold"], true);
                }
                return LoggerLevel.LOG_ALERT;
            }
            set
            {
                _Settings["EventLogThreshold"] = value.ToString();
                Save();
            }
        }

        public static string Password
        {
            get
            {
                return _Settings["Password"];
            }
            set
            {
                _Settings["Password"] = value;
                Save();
            }
        }

        public static DateTime UploadEndTime
        {
            get
            {
                return Convert.ToDateTime(_Settings["UploadEndTime"]);
            }
            set
            {
                _Settings["UploadEndTime"] = Convert.ToString(value);
                Save();
            }
        }

        public static bool UploadParts
        {
            get
            {
                return Convert.ToBoolean(_Settings["UploadParts"]);
            }
            set
            {
                _Settings["UploadParts"] = Convert.ToString(value);
                Save();
            }
        }

        public static DateTime UploadStartTime
        {
            get
            {
                return Convert.ToDateTime(_Settings["UploadStartTime"]);
            }
            set
            {
                _Settings["UploadStartTime"] = Convert.ToString(value);
                Save();
            }
        }

        public static string Username
        {
            get
            {
                return _Settings["Username"];
            }
            set
            {
                _Settings["Username"] = value;
                Save();
            }
        }

        public static DateTime WorkEndTime
        {
            get
            {
                return Convert.ToDateTime(_Settings["WorkEndTime"]);
            }
            set
            {
                _Settings["WorkEndTime"] = Convert.ToString(value);
                Save();
            }
        }

        public static DateTime WorkStartTime
        {
            get
            {
                return Convert.ToDateTime(_Settings["WorkStartTime"]);
            }
            set
            {
                _Settings["WorkStartTime"] = Convert.ToString(value);
                Save();
            }
        }
        public static int NumCPUToUse
        {
            get
            {
                return Convert.ToInt32(_Settings["NumCPUToUse"]);
            }
            set
            {
                _Settings["NumCPUToUse"] = Convert.ToString(value);
                Save();
            }

        }
    }
}

